import Vue from 'vue'
import Vuex from 'vuex'
import Cookie from 'vue-cookies'
Vue.use(Vuex)

import {
	userIndex,
} from '@/api/home'
import {
	indexCurrency,
	commonCurrencylist
} from '@/api/my'


export default new Vuex.Store({
	state: {
		timeDown: 0,
		userInfo: Cookie.get('LOGIN_USER_INFO') || {},
		lang: localStorage.getItem('lang') || (navigator.browserLanguage || navigator.language || navigator
			.userLanguage).substring(0, 2),
		currencyList: [],
		address: {}, // 转账选中地址
	},
	mutations: {
		decrementCountdown(state) {
			state.timeDown++
		},
		onChangeUserInfo(state, data) {
			state.userInfo = data
			Cookie.set('LOGIN_USER_INFO', data, 60 * 60 * 24 * 30)
			Cookie.set('TOKEN', data.token)
			state.userInfo = data
		},
		onChangeCurrency(state, data) {
			state.currencyList = data
		},
		upDataLang(state, data) {
			state.lang = data
		},
		clickTransferAddress(state, data) {
			state.address = data
		}
	},
	actions: {
		startCountdown({
			commit
		}) {
			setInterval(() => {
				commit('decrementCountdown')
			}, 1000)
		},
		async getUserInfo({
			commit
		}) {
			const res = await userIndex()
			if (res && res.code == 1) {
				// console.log('res', res);
				commit('onChangeUserInfo', res.data)
			}
		},
		// 获取币种
		async getCurrency({
			commit
		}) {
			const res = await indexCurrency()
			if (res && res.code == 1) {
				commit('onChangeCurrency', res.data)
			}
		}
	},
	modules: {}
})